<?php
//---------------------REPLACED BY CF_Sorter and CF_Header --->>>>>>
class CF_Heading {

	var $fieldspec;
	var $thetable;
	var $multiselect = false;
	
	// consider keeping locally
	var $selfpage;


// Sort variables
	var $new_order;
	var $cur_order;
	var $pagename;
	var $new_dir;
	var $cur_dir;
	var $orderstring;
	var $ordervar = 'order';	
	
	
	function CF_Heading($fieldspec, &$thetable){
		$this->thetable = &$thetable;
		$this->fieldspec = $fieldspec;
		
		//$this->multiselect = $this->thetable->multiselect;

	
	}
	
	
	/**
	 * make_simple_heading will return a heading row of the column name, in order of fieldspecs presented in fields definition
	 * @return string
	 */
	function make_simple_heading(){
		if($this->multiselect){
			$headtext .= '<th>Select</th>';
		}
		foreach($this->fieldspec AS $col_name => $colspec){
			$headtext .= '<th>';
			(empty($colspec['headname']))?$headtext .= $col_name: $headtext .= $colspec['headname'] . "</th>\n";
			}
		return $headtext;
	}

	/**
	 * make_sort_heading will return a heading where clicking on the colum name will sort by the column
	 * This requiresthe page to reload to sort the the column name through the variable specified by
	 * set_ordervar().  The return page is set in set_selfpage().
	 * @return string
	 */
	function make_sort_heading(){
		$headtext = '';
		if($this->multiselect){
			$headtext .= '<th>Select</th>';
		}
		foreach($this->fieldspec AS $col_name => $colspec){
			if(!empty($colspec['show'])){
			$headtext .= '<th>';
			(empty($colspec['headname']))?$name = $col_name: $name = $colspec['headname'] . "</th>\n";
			
			if($colspec['sort'] == true){
				$headtext .= "<a href=\"{$this->thetable->selfpage}?{$this->thetable->ordervar}=$col_name{$this->thetable->referstring}\">$name</a>";
			}
			else{
				$headtext .= $name;
			}
			$headtext .= "</th>";
			} // end if shown
			}// end foreach
			
			// add cells for actions
			if($this->thetable->allow_delete){$headtext .= "<th></th>";}
			if($this->thetable->allow_save){$headtext .= "<th></th>";}
			if($this->thetable->allow_edit){$headtext .= "<th></th>";}
					
		return $headtext;
	}	

	/**
	 *
     * get_sort_orders  Method will recieve sort order from $_REQUEST or $_SESSION
     * variables and set the internal variables for use in queries
	 * (Future) - implement this using GetPersistent
	 */
	function get_sort_orders(){
		if(isset($_SESSION[$this->thetable->table_name . '_order']) AND $_SESSION[$this->thetable->table_name . '_order'] != ''){
			$this->cur_order = $_SESSION[$this->thetable->table_name . '_order'];
			$this->cur_dir = $_SESSION[$this->thetable->table_name . '_dir'];
			
			$this->orderstring = " ORDER BY $this->cur_order  $this->cur_dir "; 
			
			}
		if(isset($_REQUEST['order']) AND $_REQUEST['order'] != ''){
			$this->new_order = $_REQUEST['order'];
			if($this->new_order == $this->cur_order){ // flip direction if clicked twice
				if($this->cur_dir == 'DESC'){
					$this->cur_dir = 'ASC';
				}
				else{
					$this->cur_dir = 'DESC';
				}
			}// End same order clicked twice
			else{
				$this->cur_order = $this->new_order;
				$this->cur_dir = 'ASC'; // default to asc
			}
		$_SESSION[$this->thetable->table_name . '_order'] = $this->cur_order;
		$_SESSION[$this->thetable->table_name . '_dir'] = $this->cur_dir;
		
		$this->orderstring = " ORDER BY $this->cur_order  $this->cur_dir "; 
		
	
		} // end order in request
	} // end get sort order


		/**
	 *  make_query_sort  Method will show the 'ORDER BY' query piece.  For Mysql, so far
	 */
	function make_query_sort(){
		$this->get_sort_orders();
		return $this->orderstring;
	
	}

	function printstuff(){
		$tn = $this->thetable->tablename;
		print "Self Page Is {$this->thetable->tablename} <pre>";
		print_r($this->thetable);
		print_r($this->fieldspec);
		print "</pre>";
	
	
	}
	
	
}


?>